﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.Serialization;
using System.IO;
using LogLib;
using ComLib.ComFun;
using System.Reflection;

namespace ComLib.ConfigService
{
    public abstract class IniConfigBase : IConfig
    {
        public virtual string ConfigDirectory
        {
            get
            {
                string directory = Path.GetDirectoryName(Assembly.GetExecutingAssembly().CodeBase);
                directory = directory.Substring(6);
                return directory;
            }
        }

        protected string _strConfigPath = "";
    
        protected string _error = "";

        protected int _nVer = 0;

        public string ErrorMessage
        {
            protected set 
            { 
                _error = value;
                LoadingError.AddErrorMessage(value);
            }
            get { return _error; }
        }

        public virtual string ConfigFilePath
        {
            get
            {
                if (string.IsNullOrEmpty(_strConfigPath))
                {
                    string className = this.GetType().Name;
                    _strConfigPath = Path.Combine(ConfigDirectory, "config", className + ".ini");
                }
                return _strConfigPath;
            }
        }

        public int nVer
        {
            get { return _nVer; }
            set { _nVer = value; }
        }

        public IniConfigBase()
        {

        }

        public virtual bool Save()
        {
            IniConfigFile.WriteValue("config", "nVer", _nVer.ToString(), ConfigFilePath);
            return true;
        }

        public virtual bool Load()
        {
            try
            {
                _nVer = int.Parse(IniConfigFile.IniValue("config", "nVer", ConfigFilePath));
                return true;
            }
            catch (Exception ex)
            {
                ErrorMessage = string.Format("配置项[nVer]加载错误: {0}", ex.Message);
                Log.WriteException(ex.ToString());
                _nVer = 0;
                return false;
            }
        }

        protected bool LoadBoolenValue(string field)
        {
            try
            {
                bool val = Convert.ToBoolean(IniConfigFile.IniValue("config", field, ConfigFilePath));
                return val;
            }
            catch (Exception ex)
            {
                ErrorMessage = string.Format("配置项[{0}]加载错误: {1}",field, ex.Message);
                Log.WriteException(ex.ToString());
                return false;
            }                   
        }

        protected int LoadInt32Value(string field)
        {
            try
            {
                 int val = Convert.ToInt32(IniConfigFile.IniValue("config", field, ConfigFilePath));
                 return val;
            }
            catch(Exception ex)
            {
                ErrorMessage = string.Format("配置项[{0}]加载错误: {1}", field, ex.Message);
                Log.WriteException(ex.ToString());
                return 0;
            }            
        }

        protected bool TryLoadInt32Value(string field, out int value)
        {
            value = 0;
            try
            {
                value = Convert.ToInt32(IniConfigFile.IniValue("config", field, ConfigFilePath));
                return true;
            }
            catch (Exception ex)
            {
                ErrorMessage = string.Format("配置项[{0}]加载错误: {1}", field, ex.Message);
                Log.WriteException(ex.ToString());
                return false;
            }
        }

        protected string LoadStringValue(string field)
        {
            return IniConfigFile.IniValue("config", field, ConfigFilePath);
        }

        protected string LoadStringValue(string group, string field)
        {
            return IniConfigFile.IniValue(group, field, ConfigFilePath);
        }

        protected T LoadEnumValue<T>(string field)
        {
            try
            {
                string text = IniConfigFile.IniValue("config", field, ConfigFilePath);
                T obj = (T)Enum.Parse(typeof(T), text);
                return obj;
            }
            catch (Exception ex)
            {
                ErrorMessage = string.Format("配置项[{0}]加载错误: {1}", field, ex.Message);
                Log.WriteException(ex.ToString());
                throw ex;
            }   
        }

        protected void SaveField(string field, object obj)
        {
            IniConfigFile.WriteValue("config", field, obj.ToString(), ConfigFilePath);
        }

        protected void SaveField(string group, string field, object obj)
        {
            IniConfigFile.WriteValue(group, field, obj.ToString(), ConfigFilePath);
        }
    }
}
